// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-08 10:50
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.DateTrain;

public class PlatesBetweenCandles {
    /**
     * 结果数组
     */
    int[] ans;

    /**
     * 获取蜡烛之间的盘子数量
     */
    public int[] platesBetweenCandles(String s, int[][] queries) {
        // 朴素遍历 - > 使用双指针进行遍历 -> 直接遍历 -> 每次遍历的数据不共享
        int len = s.length();
        if (len == 0) {
            return null;
        }
        int start = 0, end = 0, n = queries.length;
        ans = new int[n];
        int i = 0;
        for (int[] query : queries) {
            countStars(s, query[0], query[1], i++);
        }
        return ans;
    }

    /**
     * 根据给定字符串判断给定起末点计算盘子之间蜡烛的数量 -> 包含起点和终点的字符
     *
     * @param s     给定字符串
     * @param start 起点
     * @param end   终点
     */
    private void countStars(String s, int start, int end, int i) {
        int count = 0;
        while (s.charAt(start) != '|') {
            start++;
        }
        while (s.charAt(end) != '|') {
            end--;
        }
        while (start <= end) {
            if (s.charAt(start) == '*') {
                ++count;
            }
            if (start != end && s.charAt(end) == '*') {
                ++count;
            }
            ++start;
            --end;
        }
        ans[i] = count;
    }


}
